require 'rubygems'
require 'net/ssh'

if (File.exist?("commands") or File.exist?("users")) != true
  puts "You seem to be missing the commands or users file."
end

#Logs in and runs commands from the commands file.
class Main
  def initialize(hostname, username, password)
    @username = username
    @password = password
    @hostname = hostname
    dataStream
  end
  
  #This is where the connection happens
  def dataStream
     begin    
      Net::SSH.start(@hostname, @username, :password => @password) do |ssh|       
        puts "Running commands on #{@hostname} using #{@username}/#{@password}"      
        #Does the command on each line.
        File.open("commands").each do |line|        
          puts line        
          if not line.nil?           
            puts ssh.exec!(line)    
          end       
        end     
       end
        #ssh.close      
        puts "Completed all commands on #{@hostname} using #{@username}/#{@password}"
    rescue
      puts "Unable to connect to #{@hostname} using #{@username}/#{@password}"
    end
  end
end

File.open("users").each do |line|
  if not line.nil?
    user_data = line.split(":")
    Main.new(user_data[0], user_data[1], user_data[2])
  end
end